package <%= servicePackageName %>.impl;

import <%= servicePackageName %>.<%= camelCaseName(tableName,true) %>Service;
import <%= repositoryPackageName %>.<%= camelCaseName(tableName,true) %>Repository;
import <%= MapperPackageName %>.<%= camelCaseName(tableName,true) %>Mapper;
import <%= domainPackageName %>.<%= camelCaseName(tableName,true) %>;
import <%= utilsPackageName %>.CamelMap;
import <%= utilsPackageName %>.PageJpa;
import <%= utilsPackageName %>.PageMybatis;
import <%= utilsPackageName %>.PageMybatisIns;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <%= camelCaseName(tableName,true) %> <%=tableComment %>  ServiceImpl
 *
 * @author zhangQ
 * @date 2018/5/6
 */
@Service
public class <%= camelCaseName(tableName,true) %>ServiceImpl implements <%= camelCaseName(tableName,true) %>Service {
    @Autowired
    private <%= camelCaseName(tableName,true) %>Repository <%= camelCaseName(tableName) %>Repository;

    @Autowired
    private <%= camelCaseName(tableName,true) %>Mapper <%= camelCaseName(tableName) %>Mapper;

    /**
    * TODO(添加实体)
    *
    * @return
    * @author 代码生成器@zhangQ
    * @date <%= formatTime() %>
    */
    @Transactional
    @Override
    public <%= camelCaseName(tableName,true) %> saveEntity(<%= camelCaseName(tableName,true) %> <%= camelCaseName(tableName) %>) {
        return <%= camelCaseName(tableName) %>Repository.save(<%= camelCaseName(tableName) %>);
    }

<% if(existsField(table.columns,'state')) { -%>
    /**
    * TODO(修改实体状态)
    *
    * @return
    * @author 代码生成器@zhangQ
    * @date <%= formatTime() %>
    */
    @Transactional
    @Override
    public Integer <%=camelCaseName(tableName)%>ModifyState(<%= table.primaryKeys[0].type %> <%= table.primaryKeys[0].name %>, Integer state) {
        return <%= camelCaseName(tableName) %>Repository.updateByState(<%= table.primaryKeys[0].name %>, state);
    }
<% } -%>

    /**
    * Jpa分页查询
    *
    * @param page:::jpa分页工具类
    * @return
    */
    @Override
    public PageJpa<<%= camelCaseName(tableName,true) %>> query<%= camelCaseName(tableName,true) %>ByPage(PageJpa page) {
        Specification<<%= camelCaseName(tableName,true) %>> spec = (root, query, cb) -> {
            // query.where(cb.like(root.get("name").as(String.class), "%aa%")); // 例子
            return query.getRestriction();
        };
        return page.getData(<%= camelCaseName(tableName) %>Repository, spec);
    }

    /**
    * mybatis分页查询
    *
    * @param page:::mybatis分页工具
    * @return
    */
    @Override
    public PageMybatis queryMapByPage(PageMybatis page) {
        PageMybatis<Map<String, Object>> pageRes = page.getData(new PageMybatisIns() {
            @Override
            public void makeQuery(Object obj) {
                <%= camelCaseName(tableName) %>Mapper.queryMapByPage(null);
            }
        });
        return pageRes;
    }

}
